#ifndef __DYNBASE_H__
#define __DYNBASE_H__
#include <string>
class DynamicClass;
class ClassInfo;
	typedef DynamicClass* (*ObjectConstructorFn)(void);
	bool Register(ClassInfo* ci);
using namespace std;
class ClassInfo
{
public:
	ClassInfo(const char* className,ObjectConstructorFn ctor)
		:m_className(className) ,m_objectConstructor(ctor)
	{
		Register(this);
	}
	virtual ~ClassInfo(){}
	DynamicClass* CreateObject()const { return m_objectConstructor ? (*m_objectConstructor)() : 0;    }
	bool IsDynamic()const { return NULL != m_objectConstructor;}
	const char* GetClassName()const { return m_className;}
	ObjectConstructorFn GetConstructor()const{ return m_objectConstructor;}
public:
	const char* m_className;
	ObjectConstructorFn m_objectConstructor;
};

#define DECLARE_CLASS(name) \
	protected: \
		static ClassInfo ms_classinfo; \
	public:  \
		virtual ClassInfo* GetsClassInfo() const; \
		static DynamicClass* CreateObject();
#define IMPLEMENT_CLASS_COMMON(name,func) \
	ClassInfo name::ms_classinfo((#name), \
	(ObjectConstructorFn) func); \
	ClassInfo* name::GetsClassInfo() const \
{return &name::ms_classinfo;}

#define IMPLEMENT_CLASS(name)            \
	IMPLEMENT_CLASS_COMMON(name,name::CreateObject) \
	DynamicClass* name::CreateObject()                   \
	{ return new name;}

#define IMPLEMENT_NODE_CLASS(name)            \
	IMPLEMENT_CLASS_COMMON(name,name::CreateObject) \
	DynamicClass* name::CreateObject()                   \
    {return dynamic_cast<DynamicClass*>(name::create());}

class DynamicClass
{
	//DECLARE_CLASS(DynamicClass)
public:
	DynamicClass(){}
	virtual ~DynamicClass(){}
	static bool Register(ClassInfo* ci);
	static DynamicClass* CreateObject(const char* name);
};
#endif

